﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using OVS.Business;
using OVS.CoreDomain;
using OVS.CoreDomain.Business;


namespace OVS
{
    public partial class SearchCandidates : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["LoggedIn"] == null)
            {
                Response.Write("Please login!");
                Response.End();
            }
            else
            {
                ILoggedin loggedIn = (ILoggedin)Session["LoggedIn"];
                if (!loggedIn.IsAdmin)
                {
                    Response.Write("You must be logged in as Admin to view this page!");
                    Response.End();
                }
            }
        }

        protected void gvResults_OnRowCommand(object sender, GridViewCommandEventArgs e)
        {
            string candidateId;

            switch (e.CommandName)
            {
                case "EditCandidate":
                    candidateId = e.CommandArgument.ToString();
                    Response.Redirect("RegisterCandidate.aspx?Mode=Edit&CandidateId=" + candidateId);
                    break;

            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            IBusinessCandidate businessCandidate = new BusinessCandidate ();
            ICandidateSearchCriteria candidateSearchCriteria = new CandidateSearchCriteria();

            IConstituency constituency = new Constituency();
            IDistrict district = new District();
            IPanchayat panchayat = new Panchayat();
            IWard ward = new Ward();
            IElection election = new Election();
            election.ElectionId = 1;
            constituency.District = district;
            constituency.Panchayat = panchayat;
            constituency.Ward = ward;
            constituency.Election = election;


            

            IVoter voter = new Voter();
            

            candidateSearchCriteria.Voter = voter;
            candidateSearchCriteria.Constituency = constituency;


            IList<ICandidate> candidates = businessCandidate.SearchCandidates(candidateSearchCriteria);
            gvResults.DataSource = candidates;
            gvResults.DataBind();



        }
    }
}
